package com.ssm.subject.domain.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 线程池config管理
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * corePoolSize：核心线程数，超过该数量仍有任务时，会进入等待队列
     * maximumPoolSize：最大线程数，超过有界队列还有任务时，会创建救急线程(最大线程数 - 核心线程数)
     * keepAliveTIme：救急线程最大空闲时间，超过时间销毁
     * LinkedBlockingDeque：有界队列，size40
     * defaultThreadFactory：默认的线程工厂, 可使用自定义线程池
     * CallerRunsPolicy：拒绝策略，让调用者执行该任务。(超过救急线程仍有任务，会执行拒绝策略)
     */
    @Bean(name = "labelThreadPool")
    public ThreadPoolExecutor getLabelThreadPool() {
        return new ThreadPoolExecutor(20, 100, 5, TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(40),
//                Executors.defaultThreadFactory(),
                new CustomNameThreadFactory("label"),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
